<?php

namespace App\Controller\Api;

use App\Base\BaseController4Restful;
use App\Entity\Chat;
use App\Repository\ChatRepository;
use App\Repository\ConfigRepository;
use App\Service\MoneyService;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

class ApiChatController extends BaseController4Restful
{
    /**
     * @Route("/api/chats", name="apiGetChats",methods={"GET","POST"})
     */
    public function apiGetChats(ChatRepository $repository,Request $request,ConfigRepository $configRepository,MoneyService $moneyService)
    {
        switch ($request->getMethod()) {
            case "POST":
                $data = $this->getRequestData();
                /**@var $Chat Chat*/
                $Chat = $this->createEntity($data,Chat::class);
                //接收回复时需要查看其它相同信息是否处理，未处理的应该标记成已处理
                $histories = $repository->findBy(array("link"=>$Chat->getLink(),"status"=>0));
                if ($histories) {

                    //如果有未回复的表明回复有效，扣除积分
//
//                    $conf = $configRepository->findAll();
//                    $config = [];
//                    foreach ($conf as $row) {
//                        $config[$row->getK()] = $row->getV();
//                    }
//                    //保证金支付金额设置
//                    $fee = $configRepository->getByK("FEE_CHAT_MASTER");
//                    $userMoney = $moneyService->getUserMoney($Chat->getToUser());
//                    if ($userMoney["jifen"] < $fee) {
//                        return $this->json_out(400,"积分不足", $Chat);
//                    }
//                    $moneyService->mod($Chat->getToUser(),$fee * -1,MoneyService::__MONEY_JI_FEN__,202,"军师解惑");
                    foreach ($histories as $log) {
                        $log->setStatus(1);
                        $this->myEm()->persist($log);
                    }

                }
                $this->myEm()->persist($Chat);$this->myEm()->flush();
                return $this->json_out(200,"创建成功", $Chat);
            default:
                if (!$page = $this->getParamRequired("page")) {$page = 1;}
                $limit = 10;
                $offset = (int)abs($limit * ($page - 1));
                $total = $repository->count([]);
                $this->setMeta($page,$total,$limit);
                $list = $repository->findBy(array(),array(),$limit,$offset);
                return $this->json_out(200,"获取成功", $list);
        }
    }

    /**
     * @Route("/api/chats/{id}", name="apiAddChats",methods={"GET","PUT","PATCH","DELETE"})
     */
    public function apiUpdateChats(Chat $Chat,Request $request)
    {
        $data = $this->getRequestData();
        switch ($request->getMethod()) {
            case "DELETE":
                $this->myEm()->remove($Chat);$this->myEm()->flush();
                return $this->json_out(200,"已移除");
            case "PATCH":
            case "PUT":
                /**@var $Chat Chat*/
                $Chat = $this->updateEntity($data,Chat::class,$Chat);
                break;
            case "Get":
            default:
        }
        $this->myEm()->persist($Chat);$this->myEm()->flush();
        return $this->json_out(200,"获取成功", $Chat);
    }
}
